package com.ai.utils;

import java.util.List;

/**
 * 
 * @author fangll
 *
 */
public class ConvertUtils {
	
	/**
	 * 将String数组转换为Long类型数组 
	 * @param strs
	 * @return
	 */
	public static Long[] StringToLong(String[] strs){
		Long[] longArr = new Long[strs.length];
		for(int i = 0;i<strs.length;i++) {  
			String str = strs[i];       
			Long thelong = Long.valueOf(str);
			longArr[i] = thelong;
		}      
		return longArr; 		
	}
	
	/**
	 * 将Long数组转换为String类型数组 
	 * @param longs
	 * @return
	 */
	public static String[] LongToString(Long[] longs){
		String[] strArr = new String[longs.length];
		for(int i = 0;i<longs.length;i++) {  
			String str = longs[i]+"";   
			strArr[i] = str;
		}      
		return strArr; 		
	}	
	
	/**
	 * 将null转为""
	 * @param str
	 * @return
	 */
	public static String NVL(String str){
		return str==null?"":str;	
	}
	
	/**
	 * 将null转为""
	 * @param obj
	 * @return
	 */
	public static String NVL(Object obj){
		return obj==null?"":obj.toString();
	}
	
	/**
	 * obj为null时返回value,否则不变
	 * @param obj,value
	 * @return object
	 */
	public static Object NVL(Object obj,Object value){
		return obj==null?value:obj;
	}	
	
	
	/**
	 * 解析字符串为Long类型，若字符串是无效的Long型数据，则返回null，否则返回Long型数据
	 * @param sLongId
	 * @return
	 */
	public static Long parseLong(String sLong) {
		Long l = null;
		try {
			l = (sLong == null) ? null : Long.parseLong(sLong);
		} catch(Exception e) {
			l = null;
		}
		return l;
	}
	
	/**
	 * 将String列表转换成逗号分隔的字符串
	 * @param list
	 * @return
	 */
	public static String convert(List<String> list) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			sb.append(",'").append(list.get(i)).append("'");
		}
		if (sb.length() > 0) {
			return sb.substring(1);
		} else {
			return "";
		}
		
	}
	
}
